package com.newcoin.chat.service.impl;

import com.newcoin.chat.beans.vo.ResultVo;
import com.newcoin.chat.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service
@Slf4j
public class UploadServiceImpl implements UploadService {

    @Value("${host}")
    private String host;
    @Value("${chat-img}")
    private String img;//聊天图片
    @Value("${chat-voice}")
    private String voice;//聊天语音
    @Value("${circle-of-friend-path}")
    private String circleOfFriendPath;//朋友圈文件路径
    @Value("${head}")
    private String head;

    /**
     * 聊天图片语音上传
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo<String> uploadChatFile(MultipartFile file, String type) throws Exception {
        log.info("上传聊天图片语音入参file={},type={}", file, type);
        ResultVo<String> resultVo = new ResultVo<>();
        if (file.isEmpty()) {
            resultVo.setStatus(404);
            resultVo.setMsg("文件为空");
            log.info("resultVo={}", resultVo);
            return resultVo;
        }
        if (type == null) {
            resultVo.setStatus(404);
            resultVo.setMsg("类型为空");
            log.info("resultVo={}", resultVo);
            return resultVo;
        }
        String path = "";
        if (type.equalsIgnoreCase("img"))
//            path = "D:\\static\\chat\\img\\";
            path = img;
        else if (type.equalsIgnoreCase("voice"))
//            path = "D:\\static\\chat\\voice\\";
            path = voice;
        String originalFileName = file.getOriginalFilename();
        long newFileName = System.currentTimeMillis();
        String suffix = originalFileName.substring(originalFileName.indexOf(".") + 1).toLowerCase();
        File newFile = new File(path + newFileName + "." + suffix);
        file.transferTo(newFile);
        resultVo.setStatus(200);
        resultVo.setMsg("上传成功");
        resultVo.setResultData(host + ":23543/static/chat/" + type + "/" + newFileName + "." + suffix);
        log.info("resultVo={}", resultVo);
        return resultVo;
    }

    /**
     * 朋友圈文件上传
     *
     * @param files
     * @param type
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo<String> uploadCircleOfFriendFile(MultipartFile[] files, String type) throws Exception {
        log.info("朋友圈文件上传入参 files={}，type={}", files, type);
        ResultVo<String> resultVo = new ResultVo<>();
        if (files == null || type == null || type.equalsIgnoreCase("")) {
            resultVo.setMsg("文件为空");
            resultVo.setStatus(404);
            return resultVo;
        }
        String path = "";
        for (int i = 0; i < files.length; i++) {
            String originalFileName = files[i].getOriginalFilename();
            long newFileName = System.currentTimeMillis();
            String suffix = originalFileName.substring(originalFileName.indexOf(".") + 1).toLowerCase();
            File file = new File(circleOfFriendPath + type + "//" + newFileName + "_" + i + "." + suffix);
            files[i].transferTo(file);
            path = path + "," + host + ":23543/static/circleOfFriend/" + type + "/" + newFileName + "_" + i + "." + suffix;
        }
        resultVo.setResultData(path.substring(1));
        resultVo.setStatus(200);
        log.info("resultVo={}", resultVo);
        return resultVo;
    }

    /**
     * 修改头像
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo<String> uploadHeadFile(MultipartFile file) throws Exception {
        log.info("修改头像入参file={}", file);
        ResultVo<String> resultVo = new ResultVo<>();
        if (file == null) {
            resultVo.setMsg("文件为空");
            resultVo.setStatus(404);
            return resultVo;
        }
        String originalFileName = file.getOriginalFilename();
        long newFileName = System.currentTimeMillis();
        String suffix = originalFileName.substring(originalFileName.indexOf(".") + 1).toLowerCase();
        File newFile = new File(head + newFileName + "." + suffix);
        file.transferTo(newFile);
        resultVo.setStatus(200);
        resultVo.setMsg("上传成功");
        resultVo.setResultData(host + ":23543/static/head/" + newFileName + "." + suffix);
        log.info("resultVo={}", resultVo);
        return resultVo;
    }
}
